package net.ghsoft.Ctrl
{
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.events.FlexEvent;
	import mx.graphics.IFill;
	import mx.graphics.SolidColor;
	import mx.graphics.SolidColorStroke;
	
	import spark.components.Group;
	import spark.primitives.Ellipse;
	import spark.primitives.Line;
	import spark.primitives.Rect;
	import spark.primitives.supportClasses.FilledElement;
	
	public class MyWaitBar extends Group
	{
		private var _line:Line = null;
		private var _group:Group = null;
		private var _Size:int = 10;
		
		
		[Embed(source="assets/Bar/MaxOpen.png")]
		private var _MaxOpen:Class;

		[Embed(source="assets/Bar/MaxClose.png")]
		private var _MaxClose:Class;
		
		[Embed(source="assets/Bar/MinOpen.png")]
		private var _MinOpen:Class;
		
		[Embed(source="assets/Bar/MinClose.png")]
		private var _MinClose:Class;		
		
		private var _iMax:Image = new Image();
		
		private var _iMin:Image = new Image();
		
		public function MyWaitBar()
		{
			addEventListener(FlexEvent.CREATION_COMPLETE, group_creationCompleteHandler);
			super();
			
			var g:Group = new Group();
			g.percentWidth = 100;
			g.percentHeight = 100;
			var r:Rect = new Rect();
			r.percentWidth = 100;
			r.percentHeight = 100;
			r.radiusX = 10;
			r.radiusY = 10;
			r.alpha = 0.2;

			
			var sc:SolidColor = new SolidColor();
			sc.color = 0x000000;
			
			r.fill = sc;
			
			g.addElement(r);
			
			addElement(g);
			
			_group = new Group();
			_group.horizontalCenter = 0;
			var size:int = _Size;
			_group.top = size;
			_group.bottom = size + size;
			_group.width = _Size;
			addElement(_group);
			
			_line = new Line();
			_line.top = _Size;
			_line.bottom = _Size;
			_line.horizontalCenter = 0;
			_line.percentHeight = 100
			
			var hsc:SolidColorStroke = new SolidColorStroke();
			hsc.color = 0xFFFFFF;
			hsc.weight = 2;

			//_line.width = 0;
			_line.stroke = hsc;
			
			addElement(_line);

			_iMax.source = _MaxOpen;
			_iMax.horizontalCenter = 0;
			_iMax.top = 5;
			_iMax.buttonMode = true;
			_iMax.visible = _bShowMax;
			_iMax.addEventListener(MouseEvent.CLICK, Max_clickHandler);
			addElement(_iMax);
			
			_iMin.source = _MinOpen;
			_iMin.horizontalCenter = 0;
			_iMin.bottom = 5;
			_iMin.buttonMode = true;
			_iMin.visible = _bShowMin;
			_iMin.addEventListener(MouseEvent.CLICK, Min_clickHandler);
			addElement(_iMin);
		}
		
		protected function group_creationCompleteHandler(event:FlexEvent):void
		{
			if(_MinPoint > 0)
			{
				MinPoint = _MinPoint;
			}
			if(_MaxPoint > 0)
			{
				MaxPoint = _MaxPoint;
			}
			if(_NowPoint > 0)
			{
				NowPoint = _NowPoint;
			}
			if(_bShowMax == false)
			{
				ShowMax = _bShowMax;
			}
			if(_bShowMin == false)
			{
				ShowMin = _bShowMin;
			}
			
//			Alert.show(_line.height.toString());
		}
		
		private var _MaxPoint:Number = -1;
		private var _eMaxPoint:Ellipse;
		
		public function set MaxPoint(value:Number):void
		{
			_MaxPoint = value;
			
			var centre:Number = _group.height / 100 * value;
			if(_eMaxPoint == null)
			{
				_eMaxPoint = new Ellipse();
				_eMaxPoint.width = _Size;
				_eMaxPoint.height = _Size;
				_eMaxPoint.horizontalCenter = 0;
				
				var sc:SolidColor = new SolidColor();
				sc.color = 0xFF0000;
				_eMaxPoint.fill = sc;
				addElementAt(_eMaxPoint, 3);
			}
			_eMaxPoint.top = _Size + centre;
		}
		
		public function get MaxPoint():Number
		{
			return _MaxPoint;
		}
		
		private var _MinPoint:Number = -1;
		private var _eMinPoint:Ellipse;

		public function set MinPoint(value:Number):void
		{
			_MinPoint = value;
			
			var centre:Number = _group.height / 100 * value;
			
			if(_eMinPoint == null)
			{
				_eMinPoint = new Ellipse();
				_eMinPoint.width = _Size;
				_eMinPoint.height = _Size;
				_eMinPoint.horizontalCenter = 0;
				
				var sc:SolidColor = new SolidColor();
				sc.color = 0x0000FF;
				_eMinPoint.fill = sc;
				addElementAt(_eMinPoint, 3);				
			}
			_eMinPoint.top = 20 + centre;
		}
		
		
		public function get MinPoint():Number
		{
			return _MinPoint;
		}

		private var _NowPoint:Number = -1;
		private var _eNowPoint:Ellipse;
		
		public function set NowPoint(value:Number):void
		{
			_NowPoint = value;
			
			var centre:Number = _group.height / 100 * value;
			
			if(_eNowPoint == null)
			{
				_eNowPoint = new Ellipse();
				_eNowPoint.width = _Size / 2;
				_eNowPoint.height = _Size / 2;
				_eNowPoint.horizontalCenter = 0;
				
				var sc:SolidColor = new SolidColor();
				sc.color = 0xFFFF00;
				_eNowPoint.fill = sc;
				addElement(_eNowPoint);				
			}
			_eNowPoint.top = 20 + centre;
		}
		
		
		public function get NowPoint():Number
		{
			return _NowPoint;
		}
		
		private var _bMaxCheck:Boolean = true;
		
		public function set MaxCheck(value:Boolean):void
		{
			_bMaxCheck = value;
			SetMax();
		}
		
		public function get MaxCheck():Boolean
		{
			return _bMaxCheck;
		}
		
		protected function Max_clickHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			_bMaxCheck = !_bMaxCheck;
			SetMax();
		}
		
		private function SetMax():void
		{
			if(_iMax != null && _eMaxPoint != null)
			{
				if(_bMaxCheck)
				{
					_iMax.source = _MaxOpen;
					_eMaxPoint.visible = true;
				}
				else
				{
					_iMax.source = _MaxClose;
					_eMaxPoint.visible = false;
				}
			}
		}

		
		private var _bMinCheck:Boolean = true;
		
		public function set MinCheck(value:Boolean):void
		{
			_bMinCheck = value;
			SetMin();
		}
		
		public function get MinCheck():Boolean
		{
			return _bMinCheck;
		}
		
		private function SetMin():void
		{
			if(_iMin != null && _eMinPoint != null)
			{
				if(_bMinCheck)
				{
					_iMin.source = _MinOpen;
					_eMinPoint.visible = true;
				}
				else
				{
					_iMin.source = _MinClose;
					_eMinPoint.visible = false;
				}
			}
		}
		protected function Min_clickHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			_bMinCheck = !_bMinCheck;
			SetMin();
		}

		private var _bShowMax:Boolean = true;
		
		public function set ShowMax(value:Boolean):void
		{
			_bShowMax = value;
			_iMax.visible = value;
			if(_eMaxPoint != null)
			{
				if(value && _bMaxCheck && _eMaxPoint != null)
				{
					_eMaxPoint.visible = true;
				}
				else
				{
					_eMaxPoint.visible = false;
				}
			}
		}
		
		public function get ShowMax():Boolean
		{
			return _bShowMax;
		}
		
		private var _bShowMin:Boolean = true;
		
		public function set ShowMin(value:Boolean):void
		{
			_bShowMin = value;
			_iMin.visible = value;
			if(_eMinPoint != null)
			{
				if(value && _bMinCheck)
				{
					_eMinPoint.visible = true;
				}
				else
				{
					_eMinPoint.visible = false;
				}
			}
		}
		
		public function get ShowMin():Boolean
		{
			return _bShowMin;
		}
		

	}
}